package com.leased.handler;

import com.leased.core.ActionContext;
import com.leased.exception.DatabaseException;
import com.leased.exception.YCException;
import com.leased.pojo.OperateRecordInfo;
import com.leased.pojo.UserLoginLimit;
import com.leased.request.userloginlimit.UserLoginLimitRequest;
import com.leased.service.OperateRecordService;
import com.leased.service.UserLoginLimitService;
import com.leased.utils.DateTimeUtil;
import com.leased.utils.Validator;
import com.leased.utils.YCSystemStatusEnum;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by shencx on 2017/7/31.
 */
@Service
public class UserLoginLimitHandler {

    Logger LOG = LoggerFactory.getLogger(UserLoginLimitHandler.class);

    @Resource
    private UserLoginLimitService userLoginLimitService;

    @Resource
    private OperateRecordService operateRecordService;

    /**
     * 更新用户权限信息
     * @param userLoginLimitRequest
     * @throws YCException
     */
    public void modifyLoginLimit(UserLoginLimitRequest userLoginLimitRequest) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(userLoginLimitRequest, YCSystemStatusEnum.PARAM_EMPTY);
        Validator.isEmpty(userLoginLimitRequest.getId(),"主键ID不能为空");
        UserLoginLimit userLoginLimit = new UserLoginLimit();
        userLoginLimit.setId(userLoginLimitRequest.getId());
        userLoginLimit.setBeUsed(userLoginLimitRequest.getBeUsed());
        if (StringUtils.isNoneBlank(userLoginLimitRequest.getExpireTime())){
            userLoginLimit.setExpireTime(DateTimeUtil.convertDate(userLoginLimitRequest.getExpireTime()));
        }
        //是否是解绑
        if (userLoginLimitRequest.getIsUnbind() == 1){
            userLoginLimit.setHardware("");
        }
        try {
            boolean flag = userLoginLimitService.updateUserLoginLimitInfo(userLoginLimit);
            //操作成功 操作记录入库
            if (flag){
                OperateRecordInfo operateRecordInfo = new OperateRecordInfo();
                operateRecordInfo.setUserAccount(userLoginLimitRequest.getUserName());
                operateRecordInfo.setOperateName(ActionContext.getActionContext().currentUser().getName());
                operateRecordInfo.setUserType(userLoginLimitRequest.getUserType());
                //判断是车主还是专线   是修改到期时间还是解绑操作
                if (userLoginLimitRequest.getUserType() == 1){
                    if (userLoginLimitRequest.getIsUnbind() == 0){
                        operateRecordInfo.setOperateDescribe("修改了"+userLoginLimitRequest.getUserName()+"到期时间");
                    }else {
                        operateRecordInfo.setOperateDescribe("解绑"+userLoginLimitRequest.getUserName());
                    }
                }else {
                    if (userLoginLimitRequest.getIsUnbind() == 0){
                        operateRecordInfo.setOperateDescribe("修改了"+userLoginLimitRequest.getUserName()+"到期时间");
                    }else {
                        operateRecordInfo.setOperateDescribe("解绑"+userLoginLimitRequest.getUserName());
                    }
                }
                operateRecordService.addOperateRecordInfo(operateRecordInfo);

            }
        } catch (DatabaseException e) {
            LOG.error("modifyLoginLimit exception",userLoginLimit);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }

    /**
     * 获取权限列表
     * @param userId
     * @param userType
     * @return
     * @throws YCException
     */
    public List<UserLoginLimit> getLoginLimitList(Integer userId,Integer userType) throws YCException {
        /** 参数校验 */
        Validator.isEmpty(userId,"用户主键ID不能为空");
        Validator.isEmpty(userType,"用户类型不能为空");
        List<UserLoginLimit> list = null;
        try {
            list = userLoginLimitService.fetchUserLoginLimitList(userId,userType);
            return list;
        } catch (DatabaseException e) {
            LOG.error("modifyLoginLimit exception",userId,userType);
            throw new YCException(YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getCode(), YCSystemStatusEnum.INVOKE_API_RETURN_EXCEPTION.getDesc());
        }
    }
}
